package com.ktgj.other.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ktgj.common.core.annotation.Excel;
import com.ktgj.other.domain.*;
import com.ktgj.other.domain.vo.EtSceneryCollectVo;
import com.ktgj.other.domain.vo.SceneryListVo;
import com.ktgj.other.mapper.*;
import com.ktgj.other.utils.GaoDeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.other.service.IEtCollectService;

/**
 * 会员收藏景点Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-07-25
 */
@Service
public class EtCollectServiceImpl implements IEtCollectService 
{
    @Autowired
    private EtCollectMapper etCollectMapper;
    @Autowired
    private EtSceneryMapper etSceneryMapper;
    @Autowired
    private EtSceneryimageMapper etSceneryimageMapper;
    @Autowired
    private EtScenerythemeMapper etScenerythemeMapper;
    @Autowired
    private EtPartnerthemeMapper etPartnerthemeMapper;
    @Autowired
    private EtDimAreaMapper etDimAreaMapper;
    @Autowired
    private EtSceneryticketMapper etSceneryticketMapper;

    /**
     * 查询会员收藏景点
     * 
     * @param etcollectid 会员收藏景点ID
     * @return 会员收藏景点
     */
    @Override
    public EtCollect selectEtCollectById(Long etcollectid)
    {
        return etCollectMapper.selectEtCollectById(etcollectid);
    }

    /**
     * 查询会员收藏景点列表
     * 
     * @param etCollect 会员收藏景点
     * @return 会员收藏景点
     */
    @Override
    public List<EtCollect> selectEtCollectList(EtCollect etCollect)
    {
        return etCollectMapper.selectEtCollectList(etCollect);
    }

    /**
     * 新增会员收藏景点
     * 
     * @param etCollect 会员收藏景点
     * @return 结果
     */
    @Override
    public int insertEtCollect(EtCollect etCollect)
    {
        return etCollectMapper.insertEtCollect(etCollect);
    }

    /**
     * 修改会员收藏景点
     * 
     * @param etCollect 会员收藏景点
     * @return 结果
     */
    @Override
    public int updateEtCollect(EtCollect etCollect)
    {
        return etCollectMapper.updateEtCollect(etCollect);
    }

    /**
     * 批量删除会员收藏景点
     * 
     * @param etcollectids 需要删除的会员收藏景点ID
     * @return 结果
     */
    @Override
    public int deleteEtCollectByIds(Long[] etcollectids)
    {
        return etCollectMapper.deleteEtCollectByIds(etcollectids);
    }

    /**
     * 删除会员收藏景点信息
     * 
     * @param etcollectid 会员收藏景点ID
     * @return 结果
     */
    @Override
    public int deleteEtCollectById(Long etcollectid)
    {
        return etCollectMapper.deleteEtCollectById(etcollectid);
    }

    /**
     * 会员收藏景点列表
     * */
    @Override
    public List<EtSceneryCollectVo> collectList(Long id, SceneryListVo sceneryListVo) {
        GaoDeUtil gaoDeUtil = new GaoDeUtil();
        List<EtSceneryCollectVo> etSceneryCollectVoList = new ArrayList<>();
        List<EtCollect> etCollects = etCollectMapper.collectList(id);
        if (etCollects != null && etCollects.size() > 0){
            for (EtCollect etCollect : etCollects) {
                EtSceneryCollectVo etSceneryCollectVo = new EtSceneryCollectVo();
                EtScenery etScenery = etSceneryMapper.selectEtSceneryById(etCollect.getEtsceneryid());
                if (etScenery != null){
                    etSceneryCollectVo.setId(etScenery.getEtsceneryid());
                    etSceneryCollectVo.setName(etScenery.getName());

                    EtSceneryimage etSceneryimage = etSceneryimageMapper.sceneryimageUrl(etScenery.getEtsceneryid());
                    if (etSceneryimage != null){
                        etSceneryCollectVo.setUrl(etSceneryimage.getImagepath());
                    }

                    etSceneryCollectVo.setLevel(etScenery.getGrade());

                    List<String> label = new ArrayList<>();
                    List<EtScenerytheme> scenerytheme = etScenerythemeMapper.scenerytheme(etScenery.getEtsceneryid());
                    if (scenerytheme != null && scenerytheme.size() > 0){
                        for (EtScenerytheme etScenerytheme : scenerytheme) {
                            EtPartnertheme etPartnertheme = etPartnerthemeMapper.selectEtPartnerthemeById(etScenerytheme.getPartnerthemeid());
                            if (etPartnertheme != null){
                                label.add(etPartnertheme.getPartnerthemename());
                            }
                        }
                    }

                    Double aDouble = gaoDeUtil.distanceMeter(sceneryListVo.getLng(), sceneryListVo.getLat(), etScenery.getLongitude(), etScenery.getLatitude());
                    etSceneryCollectVo.setDistance(aDouble.toString());

                    EtDimArea etDimArea = etDimAreaMapper.selectEtDimAreaById(etScenery.getCityareaid());
                    if (etDimArea != null){
                        etSceneryCollectVo.setCity(etDimArea.getName());
                        EtDimArea dimArea = etDimAreaMapper.selectEtDimAreaById(etDimArea.getParentareaid());
                        if (dimArea != null){
                            etSceneryCollectVo.setProvince(dimArea.getName());
                        }
                    }

                    List<Double> priceList = new ArrayList<>();
                    List<EtSceneryticket> amount = etSceneryticketMapper.amount(etScenery.getEtsceneryid());
                    if (amount != null && amount.size() > 0){
                        for (EtSceneryticket etSceneryticket : amount) {
                            priceList.add(etSceneryticket.getMarketprice().doubleValue());
                            priceList.add(etSceneryticket.getTcamountprice().doubleValue());
                            priceList.add(etSceneryticket.getAgentprice().doubleValue());
                        }
                        etSceneryCollectVo.setReserve("true");
                    }
                    else {
                        etSceneryCollectVo.setReserve("false");
                    }

                    if (priceList != null && priceList.size() > 0){
                        priceList.sort((x, y) -> Double.compare(x, y));
                    }
                    etSceneryCollectVo.setPrice(priceList.get(0).toString());
                    etSceneryCollectVoList.add(etSceneryCollectVo);
                }
            }
        }
        return etSceneryCollectVoList;
    }

    /**
     * 收藏景点
     * */
    @Override
    public int collectAttractions(Long customerId, Long id) {
        EtCollect etCollect = new EtCollect();
        etCollect.setCustomerid(customerId);
        etCollect.setEtsceneryid(id);
        int i = etCollectMapper.insertEtCollect(etCollect);
        return i;
    }
}
